Library.WebControl.Spliter = (function() {
  //  function RegisterMove(lid, lm, mid, rid, rm, dir, cId) {
  //    var mObj = Library.WebControl.Util.GetObjectById(mid);
  //    var lObj = Library.WebControl.Util.GetObjectById(lid);
  //    var rObj = Library.WebControl.Util.GetObjectById(rid);
  //    var cObj = Library.WebControl.Util.GetObjectById(cId);
  //    var dummy = document.createElement("div");
  //    document.body.appendChild(dummy);
  //    Library.WebControl.Util.SetClass(dummy, "spliter-dummy");
  //    mObj.onmousedown = function(e) {
  //      return HandleMouseDown({
  //        Dummy: dummy,
  //        Left: lObj,
  //        LeftMin: lm,
  //        Midle: mObj,
  //        Right: rObj,
  //        RightMin: rm,
  //        Direction: dir,
  //        Container: cObj
  //      }, e);
  //    };
  //  };
  function registerMouseDown(e, obj, lm, rm, dir) {
    if (!obj.DummySpliter) {
      var dummy = document.createElement("div");
      document.body.appendChild(dummy);
      Library.WebControl.Util.SetClass(dummy, "spliter-dummy");
      obj.DummySpliter = dummy;
    }

    var util = Library.WebControl.Util;
    var mObj = util.GetObjectById(obj);
    var lObj = null;
    var rObj = null;
    var cObj = mObj.FindParent("table");
    switch (dir) {
      case 0: // horizon
        lObj = mObj.PreviewElement("td");
        rObj = mObj.NextElement("td");

        break;
      case 1: // vertical
        lObj = mObj.PreviewElement("tr");
        rObj = mObj.NextElement("tr");
        break;
    }
    return HandleMouseDown({
      Dummy: obj.DummySpliter,
      Left: lObj,
      LeftMin: lm,
      Midle: mObj,
      Right: rObj,
      RightMin: rm,
      Direction: dir,
      Container: cObj
    }, e);
  };
  function HandleMouseMove(s, e) {
    var mosPos = Library.WebControl.Util.GetMousePosition(e);
    if (s.Direction == 0) { // horizon
      s.Dummy.style.left = mosPos.X + "px";
    }
    else
      if (s.Direction == 1) { // vertical
      s.Dummy.style.top = mosPos.Y + "px";
    }
    if (document.selection) {
      document.selection.clear();
    }
  };
  function AdaptSize(dSize, dMin, v, iSize) {
    var d = dSize - v;
    if (d < dMin) {
      v -= dMin - d;
      d = dMin;
    }
    var i = iSize + v;
    return [d, i];

  };
  function HandleMouseUp(s, e) {
    s.Dummy.style.visibility = "hidden";
    document.onmousemove = null;
    document.onmouseup = null;
    document.onselectstart = null;
    var lSize = Library.WebControl.Util.GetSize(s.Left);
    var rSize = Library.WebControl.Util.GetSize(s.Right);
    var mosPos = Library.WebControl.Util.GetMousePosition(e);
    var spPos = Library.WebControl.Util.GetPosition(s.Midle);
    var offset = 0;
    if (s.Direction == 0) { // horizon
      //						s.Dummy.style.left = mosPos.X + "px";
      //						return;
      offset = spPos.X - mosPos.X;
      if (offset > 0) {//move left           
        if (lSize.W > s.LeftMin) {
          var adSize = AdaptSize(lSize.W, s.LeftMin, offset, rSize.W);
          var owl = s.Left.style.width;
          var owr = s.Right.style.width;
          s.Left.style.width = adSize[0] + "px";
          s.Right.style.width = adSize[1] + "px";
          var size = Library.WebControl.Util.GetSize(s.Container);
          if (size.W != s.ContainerSize.W) {
            s.Right.style.width = owr;
            s.Left.style.width = owl;
          }


        }
      }
      else {// move right          
        if (rSize.W > s.RightMin) {
          var adSize = AdaptSize(rSize.W, s.RightMin, -offset, lSize.W);
          var owl = s.Left.style.width;
          var owr = s.Right.style.width;
          s.Right.style.width = adSize[0] + "px";
          s.Left.style.width = adSize[1] + "px";
          var size = Library.WebControl.Util.GetSize(s.Container);
          if (size.W != s.ContainerSize.W) {
            s.Right.style.width = owr;
            s.Left.style.width = owl;
          }

        }
      }


    }
    else
      if (s.Direction == 1) { // vertical
      //								s.Dummy.style.top = mosPos.Y + "px";
      //								return;
      offset = spPos.Y - mosPos.Y;
      if (offset > 0) {//move top           
        if (lSize.H > s.LeftMin) {
          var adSize = AdaptSize(lSize.H, s.LeftMin, offset, rSize.H);
          var ohl = s.Left.style.height;
          var ohr = s.Right.style.height;
          s.Left.style.height = adSize[0] + "px";
          s.Right.style.height = adSize[1] + "px";
          var size = Library.WebControl.Util.GetSize(s.Container);
          if (size.H != s.ContainerSize.H) {
            s.Left.style.height = ohl;
            s.Right.style.height = ohr;
          }


        }
      }
      else {// move bottom          
        if (rSize.H > s.RightMin) {
          var adSize = AdaptSize(rSize.H, s.RightMin, -offset, lSize.H);
          var ohl = s.Left.style.height;
          var ohr = s.Right.style.height;
          s.Right.style.height = adSize[0] + "px";
          s.Left.style.height = adSize[1] + "px";
          var size = Library.WebControl.Util.GetSize(s.Container);
          if (size.H != s.ContainerSize.H) {
            s.Left.style.height = ohl;
            s.Right.style.height = ohr;
          }


        }
      }
    }

  };
  function HandleMouseDown(s, e) {
    var evt = Library.WebControl.Util.GetEvent(e);
    var mouseButton;
    if (evt.which) {
      mouseButton = evt.which;
    }
    else {
      mouseButton = evt.button;
    }
    if (mouseButton == 1) {
      s.ContainerSize = Library.WebControl.Util.GetSize(s.Container);

      var mdP = Library.WebControl.Util.GetPosition(s.Midle);
      var mdS = Library.WebControl.Util.GetSize(s.Midle);
      s.Dummy.style.width = mdS.W + "px";
      s.Dummy.style.height = mdS.H + "px";
      s.Dummy.style.top = mdP.Y + "px";
      s.Dummy.style.left = mdP.X + "px";
      s.Dummy.style.visibility = "visible";
      document.onmousemove = function(moEvt) {
        HandleMouseMove(s, moEvt);
      };
      document.onmouseup = function(mu) { HandleMouseUp(s, mu); };
    }
    // cancel out any text selections
    document.body.focus();

    // prevent text selection in IE
    document.onselectstart = function() { return false; };
    return false;
  };

  return {
    //Register: RegisterMove
    Register: registerMouseDown
  };
} ());

